import dayjs from 'dayjs'
import { Logger } from '../../utils/index'

const logger = new Logger()
/**
 * 获取班次列表中的流水班
 * @param {Array} voyageList 班次列表
 * @returns {Array} 返回流水班列表
 */
const getSerialVoyageList = (voyageList) => {
    return voyageList.filter(item => item.IsSerialVoyage && item.IsCompositeProduct === 0)
}

/**
 * 获取班次列表中的组合产品班次
 * @param {Array} voyageList 班次列表
 * @returns {Array} 返回组合产品班次列表 
 */
const getCombinationProductList = (voyageList) => {
    return voyageList.filter(item => item.IsCompositeProduct === 1)
}

/**
 * 获取班次列表中的普通班次（不包含流水班、组合产品班次）
 * @param {Array} voyageList 班次列表
 * @returns {Array} 返回普通班次列表
 */
const getNormalList = (voyageList) => {
    return voyageList.filter(item => !item.IsSerialVoyage && item.IsCompositeProduct === 0)
}

/**
 * 获取班次列表中的无票班次
 * @param {Array} voyageList 班次列表
 * @returns {Array} 返回无票班次列表 
 */
const getNoTicketList = (voyageList) => {
    return voyageList.filter(item => {
        const isNoTicketForMain = !item.HaveTicket // 主供应商是否无票
        let isNoTicketForChild = true // 子供应商是否无票
        if (item.MergeVoyages && item.MergeVoyages.length > 0) {
            isNoTicketForChild = item.MergeVoyages.every(childVoyage => !childVoyage.HaveTicket)
        }
        return isNoTicketForMain && isNoTicketForChild
    })
}

/**
 * 获取班次列表中的停售班次
 * @param {Array} voyageList 班次列表
 * @returns {Array} 返回停售班次列表 
 */
const getCanNotBookingList = (voyageList) => {
    return voyageList.filter(item => {
        const isCanNotBookingForMain = !item.CanBooking // 主供应商是否停售
        let isCanNotBookingForChild = true // 子供应商是否停售
        if (item.MergeVoyages && item.MergeVoyages.length > 0) {
            isCanNotBookingForChild = item.MergeVoyages.every(childVoyage => !childVoyage.CanBooking)
        }
        return isCanNotBookingForMain && isCanNotBookingForChild
    })
}

/**
 * 获取班次列表中的有票且可预订的班次
 * @param {Array} voyageList 班次列表
 * @returns {Array} 返回有票且可预订的班次列表
 */
const getActiveList = (voyageList) => {
    return voyageList.filter(item => {
        const isHaveTicketForMain = item.HaveTicket // 主供应商是否有票
        let isHaveTicketForChild = false // 子供应商是否有票
        const isCanBookingForMain = item.CanBooking // 主供应商是否可售
        let isCanBookingForChild = false // 子供应商是否可售
        let isHaveTicket // 是否有票
        let isCanBooking // 是否可售
        if (item.MergeVoyages && item.MergeVoyages.length > 0) {
            isHaveTicketForChild = item.MergeVoyages.some(childVoyage => childVoyage.HaveTicket)
            isCanBookingForChild = item.MergeVoyages.some(childVoyage => childVoyage.CanBooking)
        }
        isHaveTicket = isHaveTicketForMain || isHaveTicketForChild
        isCanBooking = isCanBookingForMain || isCanBookingForChild
        return isHaveTicket && isCanBooking
    })
}

/**
 * 获取班次列表中的不可预订的班次
 * @param {Array} voyageList 班次列表 
 * @returns {Array} 返回不可预订班次列表
 */
const getDisabledList = (voyageList) => {
    return voyageList.filter(item => {
        const isNoTicketForMain = !item.HaveTicket // 主供应商是否无票
        let isNoTicketForChild = true // 子供应商是否无票
        const isCanNotBookingForMain = !item.CanBooking // 主供应商是否停售
        let isCanNotBookingForChild = true // 子供应商是否停售
        let isNoTicket // 是否无票
        let isCanNotBooking // 是否停售
        if (item.MergeVoyages && item.MergeVoyages.length > 0) {
            isNoTicketForChild = item.MergeVoyages.every(childVoyage => !childVoyage.HaveTicket)
            isCanNotBookingForChild = item.MergeVoyages.every(childVoyage => !childVoyage.CanBooking)
        }
        isNoTicket = isNoTicketForMain && isNoTicketForChild
        isCanNotBooking = isCanNotBookingForMain && isCanNotBookingForChild
        return isNoTicket || isCanNotBooking
    })
}


  
  /**
   * 生成班次服务标签列表
   * @returns {Array} 标签列表
   */
  function _getServiceTagList (schedule) {
    const tagList = []
    if (schedule.Refundable) {
        tagList.push('可在线退票')
    }
    if (schedule.SupportChildrenTicket) {
        tagList.push('儿童票')
    }

    // FIXME: 暂不支持学生票
    schedule.SupportStudentTicket = false
    if (schedule.SupportStudentTicket) {
        tagList.push('学生票')
    }
    if (schedule.SupportOldTicket) {
        tagList.push('长者票')
    }
    return tagList
  }

export function clearLineData(lines, isRoundTrip) {
    // 洗数据  流水班  组合产品， 停航停售  余票不足  多供应商 排序等等
      //  组合产品 判断条件  IsCompositeProduct === 1
      // 流水班判断条件  IsSerialVoyage true
      // 停航停售 判断条件   CanBooking false
      // 余票不足判断条件  HaveTicket false
      lines.forEach(item => {
        item.isComProduct = item.IsCompositeProduct === 1
        item.isSerial = item.IsSerialVoyage
        item.disabled = !item.CanBooking || item.MinPrice <= 0
        item.disallowTxt = !item.CanBooking? '停售': !item.HaveTicket? '无票': ''

        // 游船线路
        item.isSightseeingBoat = !item.IsSerialVoyage && (item.IsShowBoat || item.NeedTripExplain)

        item.isCombined = item.RenderingType == 1

        item.isVehicle = item.SupportVehicle && !item.IsShowBoat && !item.NeedTripExplain

        item.lineTag = item.isCombined? '车船联运': item.isVehicle? '带车上船': item.isSightseeingBoat? '游船线路': ''

        item.lineRightTag = item.IsReseverVoyage? '预约购票': ''
        item.orderBtnTxt = item.IsReseverVoyage? '预约': '预订'

        // 时间 格式化
        item.startTime = dayjs(item.DepartDateTime).format('HH:mm')
        item.arriveTime = dayjs(item.ArriveDateTime).format('HH:mm')

        item.tagList = _getServiceTagList(item)


        // 多供应商
        item.isShowMultiSupplier = item.IsShowMultiSupplier && item.MergeVoyages && item.MergeVoyages.length > 0 && !isRoundTrip
        item.isMultiSupplierSlide = false
        item.isShowSeatList = false
        
        if (item.isShowMultiSupplier) { // 有子供应商才做合并
          // 主供应商
          const mainVoyage = {
              VoyageSerialNo: item.VoyageSerialNo,
              SupplierId: item.SupplierId,
              SupplierName: item.SupplierName,
              LineId: item.LineId,
              LineCode: item.LineCode,
              LineName: item.LineName,
              VoyageId: item.VoyageId,
              ShipCode: item.ShipCode,
              DepartDateTime: item.DepartDateTime,
              ArriveDateTime: item.ArriveDateTime,
              LineTag: item.LineTag,
              MinPrice: item.MinPrice,
              HaveTicket: item.HaveTicket,
              CanBooking: item.CanBooking,
              Activitytag: item.Activitytag,
              DiscountPrice: item.DiscountPrice,
            //   IsUseNewVoyagePage: item.IsUseNewVoyagePage,
              SupportChildrenTicket: item.SupportChildrenTicket,
              SupportOldTicket: item.SupportOldTicket,
              SupportVehicle: item.SupportVehicle,
              SupportStudent: item.SupportStudent,
              Refundable: item.Refundable,
              SupplierAliasName: item.SupplierAliasName,
              ShipSeats: item.ShipSeats,
              isMainVoyage: true,
              supplierDisplayName: item.SupplierAliasName || item.SupplierName,
              tagList: item.LineTag.split(','),
              ShipName: item.ShipName,
              isShowMultiSupplier: item.isShowMultiSupplier
          }
          item.MergeVoyages.unshift(mainVoyage)
          
          // 多供应商列表添加展示名称和标签
          item.MergeVoyages.forEach(element => {
              element.tagList = element.LineTag.split(',')
              if (element.isMainVoyage) { // 主供应商
                  element.supplierDisplayName = element.SupplierAliasName || element.SupplierName
              } else {
                  element.supplierDisplayName = element.SupplierAliasName || '苏州创旅'
              }
              if (!element.CanBooking || !element.HaveTicket) {
                  element.BrokerSort = -1
              }

              // 子供应商没有的参数 补充下
              element.DepartCity = item.DepartCity
              element.ArriveCity = item.ArriveCity
              element.DepartPort = item.DepartPort

              element.disabled = !element.CanBooking || !element.HaveTicket
          })
          item.MergeVoyages.sort(function (a, b) {
              return b.BrokerSort - a.BrokerSort
          })
          // TODO 根据BrokerSort排序
          logger.info(`多供应商线路： ${item.startTime}->${item.arriveTime}`)
        }
        
        if(item.SupportHuoche === 1) {
          logger.info(`支持货车： ${item.startTime}->${item.arriveTime}`)
        }



      })

    return lines
}


export {
    getSerialVoyageList,
    getCombinationProductList,
    getNormalList,
    getNoTicketList,
    getCanNotBookingList,
    getActiveList,
    getDisabledList
}